<html>
 <head>
  <meta charset="utf-8"/>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"/>
  <title>
   浅谈：协同过滤算法CF  | 数螺 | NAUT IDEA
  </title>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" rel="stylesheet"/>
  <link href="http://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
  <style type="text/css">
   #xmain img {
                  max-width: 100%;
                  display: block;
                  margin-top: 10px;
                  margin-bottom: 10px;
                }

                #xmain p {
                    line-height:150%;
                    font-size: 16px;
                    margin-top: 20px;
                }

                #xmain h2 {
                    font-size: 24px;
                }

                #xmain h3 {
                    font-size: 20px;
                }

                #xmain h4 {
                    font-size: 18px;
                }


                .header {
	           background-color: #0099ff;
	           color: #ffffff;
	           margin-bottom: 20px;
	        }

	        .header p {
                  margin: 0px;
                  padding: 10px 0;
                  display: inline-block;  
                  vertical-align: middle;
                  font-size: 16px;
               }

               .header a {
                 color: white;
               }

              .header img {
                 height: 25px;
              }
  </style>
  <script src="http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js">
  </script>
  <script src="http://nautstatic-10007657.file.myqcloud.com/static/css/readability.min.js" type="text/javascript">
  </script>
  <script type="text/javascript">
   $(document).ready(function() {
                 var loc = document.location;
                 var uri = {
                  spec: "http://dataunion.org/13503.html",
                  host: "http://dataunion.org",
                  prePath: "http://dataunion.org",
                  scheme: "http",
                  pathBase: "http://dataunion.org/"
                 };
    
                 var documentClone = document.cloneNode(true);
                 var article = new Readability(uri, documentClone).parse();
     
                 document.getElementById("xmain").innerHTML = article.content;
                });
  </script>
  <!-- 1466461487: Accept with keywords: (title(0.2):算法,协同,社区,浅谈,数盟, topn(0.233333333333):社区,浅谈,职业规划,接下来,数盟,行业资讯,数组,数据挖掘,数据格式,数量,文件,协同,题目,电影,引擎,基础架构,计算,文章,物品,排序,数据,字典,评分,用户,算法,链接,网站,列表,文件格式,归一化).-->
 </head>
 <body onload="">
  <div class="header">
   <div class="container">
    <div class="row">
     <div class="col-xs-6 col-sm-6 text-left">
      <a href="/databee">
       <img src="http://nautidea-10007657.cos.myqcloud.com/logo_white.png"/>
      </a>
      <a href="/databee">
       <p>
        数螺
       </p>
      </a>
     </div>
     <div class="hidden-xs col-sm-6 text-right">
      <p>
       致力于数据科学的推广和知识传播
      </p>
     </div>
    </div>
   </div>
  </div>
  <div class="container text-center">
   <h1>
    浅谈：协同过滤算法CF
   </h1>
  </div>
  <div class="container" id="xmain">
   ﻿﻿
   <title>
    浅谈：协同过滤算法CF | 数盟社区
   </title>
   <!-- All in One SEO Pack 2.2.7.6.2 by Michael Torbert of Semper Fi Web Design[32,58] -->
   <!-- /all in one seo pack -->
   <!--
<div align="center">
<a href="http://strata.oreilly.com.cn/hadoop-big-data-cn?cmp=mp-data-confreg-home-stcn16_dataunion_pc" target="_blank"><img src="http://dataunion.org/wp-content/uploads/2016/05/stratabj.jpg"/ ></a>
</div>
-->
   <header id="header-web">
    <div class="header-main">
     <hgroup class="logo">
      <h1>
       <a href="http://dataunion.org/" rel="home" title="数盟社区">
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/logo.png"/>
       </a>
      </h1>
     </hgroup>
     <!--logo-->
     <nav class="header-nav">
      <ul class="menu" id="menu-%e4%b8%bb%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-71" id="menu-item-71">
        <a href="http://dataunion.org/category/events" title="events">
         活动
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22457" id="menu-item-22457">
          <a href="http://dataunion.org/2016timeline">
           2016档期
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22459" id="menu-item-22459">
          <a href="http://dataunion.org/category/parterc">
           合作会议
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-20869" id="menu-item-20869">
        <a href="http://dataunion.org/category/tech" title="articles">
         文章
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20867" id="menu-item-20867">
          <a href="http://dataunion.org/category/tech/base" title="base">
           基础架构
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3302" id="menu-item-3302">
          <a href="http://dataunion.org/category/tech/ai" title="ai">
           人工智能
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3303" id="menu-item-3303">
          <a href="http://dataunion.org/category/tech/analysis" title="analysis">
           数据分析
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21920" id="menu-item-21920">
          <a href="http://dataunion.org/category/tech/dm">
           数据挖掘
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3314" id="menu-item-3314">
          <a href="http://dataunion.org/category/tech/viz" title="viz">
           可视化
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-3305" id="menu-item-3305">
          <a href="http://dataunion.org/category/tech/devl" title="devl">
           编程语言
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-20876" id="menu-item-20876">
        <a href="http://dataunion.org/category/industry">
         行业
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-16328" id="menu-item-16328">
          <a href="http://dataunion.org/category/industry/case" title="case">
           行业应用
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-2112" id="menu-item-2112">
          <a href="http://dataunion.org/category/industry/demo" title="demo">
           Demo展示
          </a>
         </li>
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21562" id="menu-item-21562">
          <a href="http://dataunion.org/category/industry/news">
           行业资讯
          </a>
         </li>
        </ul>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-311" id="menu-item-311">
        <a href="http://dataunion.org/category/sources" title="sources">
         资源
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20870" id="menu-item-20870">
        <a href="http://dataunion.org/category/books" title="book">
         图书
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-21363" id="menu-item-21363">
        <a href="http://dataunion.org/category/training">
         课程
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-has-children menu-item-21853" id="menu-item-21853">
        <a href="http://dataunion.org/category/jobs">
         职位
        </a>
        <ul class="sub-menu">
         <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-22050" id="menu-item-22050">
          <a href="http://dataunion.org/category/career">
           职业规划
          </a>
         </li>
        </ul>
       </li>
      </ul>
     </nav>
     <!--header-nav-->
    </div>
   </header>
   <!--header-web-->
   <div id="main">
    <div id="soutab">
     <form action="http://dataunion.org/" class="search" method="get">
     </form>
    </div>
    <div id="container">
     <nav id="mbx">
      当前位置：
      <a href="http://dataunion.org">
       首页
      </a>
      &gt;
      <a href="http://dataunion.org/category/tech">
       文章
      </a>
      &gt;  正文
     </nav>
     <!--mbx-->
     <article class="content">
      <header align="centre" class="contenttitle">
       <div class="mscc">
        <h1 class="mscctitle">
         <a href="http://dataunion.org/13503.html">
          浅谈：协同过滤算法CF
         </a>
        </h1>
        <address class="msccaddress ">
         <em>
          5,404 次阅读 -
         </em>
         <a href="http://dataunion.org/category/tech" rel="category tag">
          文章
         </a>
        </address>
       </div>
      </header>
      <div class="content-text">
       <p>
        作者：ACdreamers
       </p>
       <p>
        今天要讲的主要内容是
        <strong>
         协同过滤
        </strong>
        ，即Collaborative Filtering，简称
        <strong>
         CF
        </strong>
        。
       </p>
       <p>
        <strong>
         Contents
        </strong>
       </p>
       <p>
        <strong>
         1. 协同过滤的简介
        </strong>
       </p>
       <p>
        <strong>
         2. 协同过滤的核心
        </strong>
       </p>
       <p>
        <strong>
         3. 协同过滤的实现
        </strong>
       </p>
       <p>
        <strong>
         4. 协同过滤的应用
        </strong>
       </p>
       <p>
       </p>
       <p>
        <strong>
         1. 协同过滤的简介
        </strong>
       </p>
       <p>
        关于协同过滤的一个最经典的例子就是看电影，有时候不知道哪一部电影是我们喜欢的或者评分比较高的，那么通常的做法就是问问周围的朋友，看看最近有什么好的电影推荐。在问的时候，都习惯于问跟自己口味差不多的朋友，这就是协同过滤的核心思想。
       </p>
       <p>
        协同过滤是在海量数据中挖掘出小部分与你品味类似的用户，在协同过滤中，这些用户成为邻居，然后根据他们喜欢的东西组织成一个排序的目录推荐给你。所以就有如下两个核心问题
       </p>
       <p>
        （1）如何确定一个用户是否与你有相似的品味？
       </p>
       <p>
        （2）如何将邻居们的喜好组织成一个排序目录？
       </p>
       <p>
        协同过滤算法的出现标志着推荐系统的产生，协同过滤算法包括基于用户和基于物品的协同过滤算法。
       </p>
       <p>
        <strong>
         2. 协同过滤的核心
        </strong>
       </p>
       <p>
        要实现协同过滤，需要进行如下几个步骤
       </p>
       <p>
        （1）收集用户偏好
       </p>
       <p>
        （2）找到相似的用户或者物品
       </p>
       <p>
        （3）计算并推荐
       </p>
       <p>
        <strong>
         收集用户偏好
        </strong>
       </p>
       <p>
        从用户的行为和偏好中发现规律，并基于此进行推荐，所以如何收集用户的偏好信息成为系统推荐效果最基础的决定因素。用户有很多种方式向系统提供自己的偏好信息，比如：评分，投票，转发，保存书签，购买，点击流，页面停留时间等等。
       </p>
       <p>
        以上的用户行为都是通用的，在实际推荐引擎设计中可以自己多添加一些特定的用户行为，并用它们表示用户对物品的喜好程度。通常情况下，在一个推荐系统中，用户行为都会多于一种，那么如何组合这些不同的用户行为呢 ？基本上有如下两种方式
       </p>
       <p>
        （1）将不同的行为分组
       </p>
       <p>
        一般可以分为查看和购买，然后基于不同的用户行为，计算不同用户或者物品的相似度。类似与当当网或者亚马逊给出的“购买了该书的人还购买了”，“查看了该书的人还查看了”等等。
       </p>
       <p>
        （2）不同行为产生的用户喜好对它们进行加权对不同行为产生的用户喜好进行加权，然后求出用户对物品的总体喜好。
       </p>
       <p>
        好了，当我们收集好用户的行为数据后，还要对数据进行预处理，最核心的工作就是
        <strong>
         减噪
        </strong>
        和
        <strong>
         归一化
        </strong>
        。
       </p>
       <p>
        <strong>
         减噪：
        </strong>
        因为用户数据在使用过程中可能存在大量噪音和误操作，所以需要过滤掉这些噪音。
       </p>
       <p>
        <strong>
         归一化：
        </strong>
        不同行为数据的取值相差可能很好，例如用户的查看数据肯定比购买数据大得多。通过归一化，才能使数据更加准确。
       </p>
       <p>
        通过上述步骤的处理，就得到了一张二维表，其中一维是用户列表，另一维是商品列表，值是用户对商品的喜好。还是以电影推荐为例，如下表
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        <strong>
         找到相似的用户或物品
        </strong>
       </p>
       <p>
        对用户的行为分析得到用户的喜好后，可以根据用户的喜好计算相似用户和物品，然后可以基于相似用户或物品进行推荐。这就是协同过滤中的两个分支了，基于用户的和基于物品的协同过滤。
       </p>
       <p>
        关于相似度的计算有很多种方法，比如常用的余弦夹角，欧几里德距离度量，皮尔逊相关系数等等。而如果采用欧几里德度量，那么可以用如下公式来表示相似度
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        在计算用户之间的相似度时，是将一个用户对所有物品的偏好作为一个向量，而在计算物品之间的相似度时，是将所有用户对某个物品的偏好作为一个向量。求出相似度后，接下来可以求相似邻居了。
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        <strong>
         计算并推荐
        </strong>
       </p>
       <p>
       </p>
       <p>
        在上面，我们求出了相邻用户和相邻物品，接下来就应该进行推荐了。当然从这一步开始，分为两方面，分别是基于用户的协同过滤和基于物品的协同过滤。我会分别介绍它们的原理
       </p>
       <p>
       </p>
       <p>
        （1）基于用户的协同过滤算法
       </p>
       <p>
       </p>
       <p>
        在上面求相似邻居的时候，通常是求出TOP K邻居，然后根据邻居的相似度权重以及它们对物品的偏好，预测当前用户没有偏好的未涉及物品，计算得到一个排序的物品列表进行推荐。
       </p>
       <p>
       </p>
       <p>
        （2）基于物品的协同过滤算法
       </p>
       <p>
       </p>
       <p>
        跟上述的基于用户的协同过滤算法类似，但它从物品本身，而不是用户角度。比如喜欢物品A的用户都喜欢物品C，那么可以知道物品A与物品C的相似度很高，而用户C喜欢物品A，那么可以推断出用户C也可能喜欢物品C。如下图
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        上面的相似度权重有时候需要加入惩罚因子，举个例子，在日常生活中，我们每个人购买卫生纸的的频率比较高，但是不能说明这些用户的兴趣点相似，但是如果它们都买了照相机，那么就可以大致推出它们都是摄影爱好者。所以像卫生纸这样的物品在计算时，相似度权重需要加上惩罚因子或者干脆直接去掉这类数据。
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        <strong>
         适用场景
        </strong>
       </p>
       <p>
       </p>
       <p>
        对于一个在线网站，用户的数量往往超过物品的数量，同时物品数据相对稳定，因此计算物品的相似度不但计算量小，同时不必频繁更新。但是这种情况只适用于电子商务类型的网站，像新闻类，博客等这类网站的系统推荐，情况往往是相反的，物品数量是海量的，而且频繁更新。所以从算法复杂度角度来说，两种算法各有优势。关于协同过滤的文章，可以参考这里：
        <a href="http://www.tuicool.com/articles/6vqyYfR" target="_blank">
         <strong>
          http://www.tuicool.com/articles/6vqyYfR
         </strong>
        </a>
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        <strong>
         3. 协同过滤的实现
        </strong>
       </p>
       <p>
       </p>
       <p>
        上面已经介绍了协同过滤的核心思想，现在就来实战一下吧！ 采用数据集如下
       </p>
       <p>
       </p>
       <p>
        <strong>
         链接：
        </strong>
        <a href="http://grouplens.org/datasets/movielens/" target="_blank">
         <strong>
          http://grouplens.org/datasets/movielens/
         </strong>
        </a>
       </p>
       <p>
       </p>
       <p>
        这个数据集是很多用户对各种电影的评分。接下来先采用Python实现基于用户的协同过滤算法。
       </p>
       <p>
       </p>
       <p>
        首先，我们需要以表格形式读取数据，需要用到Texttable第三方包。安装包如下链接
       </p>
       <p>
       </p>
       <p>
        <strong>
         链接：
        </strong>
        <a href="https://pypi.python.org/pypi/texttable/" target="_blank">
         <strong>
          https://pypi.python.org/pypi/texttable/
         </strong>
        </a>
       </p>
       <p>
       </p>
       <p>
        大致用法如下
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        更多方法的使用需要参考Textdtable的源文件texttable.py。接下来可以实现协同过滤算法了。
       </p>
       <p>
       </p>
       <p>
        <strong>
         代码：
        </strong>
       </p>
       <!-- Crayon Syntax Highlighter v_2.7.2_beta -->
       <div class="crayon-syntax crayon-theme-classic crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover" id="crayon-57686d2c90495583695634" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
        <div class="crayon-toolbar" data-settings=" mouseover overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
         <span class="crayon-title">
         </span>
         <div class="crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
          <div class="crayon-button crayon-nums-button" title="切换是否显示行编号">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-plain-button" title="纯文本显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-wrap-button" title="切换自动换行">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-expand-button" title="点击展开代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-copy-button" title="复制代码">
           <div class="crayon-button-icon">
           </div>
          </div>
          <div class="crayon-button crayon-popup-button" title="在新窗口中显示代码">
           <div class="crayon-button-icon">
           </div>
          </div>
         </div>
        </div>
        <div class="crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
        </div>
        <div class="crayon-plain-wrap">
         <textarea class="crayon-plain print-no" data-settings="dblclick" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;" wrap="soft">
          # -*-coding=utf-8 -*-

import sys
import math
from texttable import Texttable

#计算余弦距离
def getCosDist(user1, user2):
	sum_x = 0.0
	sum_y = 0.0
	sum_xy = 0.0
	for key1 in user1:
		for key2 in user2:
			if key1[0] == key2[0]:
				sum_x += key1[1] * key1[1]
				sum_y += key2[1] * key2[1]
				sum_xy += key1[1] * key2[1]
	if sum_xy == 0.0:
		return 0
	demo = math.sqrt(sum_x * sum_y)
	return sum_xy / demo

#读取文件，读取以行为单位，每一行是列表里的一个元素
def readFile(filename):
	contents = []
	f = open(filename, "r")
	contents = f.readlines()
	f.close()
	return contents

#数据格式化为二维数组
def getRatingInfo(ratings):
	rates = []
	for line in ratings:
		rate = line.split("\t")
		rates.append([int(rate[0]), int(rate[1]), int(rate[2])])
	return rates

#生成用户评分数据结构
def getUserScoreDataStructure(rates):
	
	#userDict[2]=[(1,5),(4,2)].... 表示用户2对电影1的评分是5，对电影4的评分是2
	userDict = {}
	itemUser = {}
	for k in rates:
		user_rank = (k[1], k[2])
		if k[0] in userDict:
			userDict[k[0]].append(user_rank)
		else:
			userDict[k[0]] = [user_rank]

		if k[1] in itemUser:
			itemUser[k[1]].append(k[0])
		else:
			itemUser[k[1]] = [k[0]]
	return userDict, itemUser

#计算与指定用户最相近的邻居
def getNearestNeighbor(userId, userDict, itemUser):
	neighbors = []
	for item in userDict[userId]:
		for neighbor in itemUser[item[0]]:
			if neighbor != userId and neighbor not in neighbors:
				neighbors.append(neighbor)
	neighbors_dist = []
	for neighbor in neighbors:
		dist = getCosDist(userDict[userId], userDict[neighbor])
		neighbors_dist.append([dist, neighbor])
	neighbors_dist.sort(reverse = True)
	return neighbors_dist

#使用UserFC进行推荐，输入：文件名,用户ID,邻居数量
def recommendByUserFC(filename, userId, k = 5):
	
	#读取文件
	contents = readFile(filename)

	#文件格式数据转化为二维数组
	rates = getRatingInfo(contents)

	#格式化成字典数据
	userDict, itemUser = getUserScoreDataStructure(rates)

	#找邻居
	neighbors = getNearestNeighbor(userId, userDict, itemUser)[:5]

	#建立推荐字典
	recommand_dict = {}
	for neighbor in neighbors:
		neighbor_user_id = neighbor[1]
		movies = userDict[neighbor_user_id]
		for movie in movies:
			if movie[0] not in recommand_dict:
				recommand_dict[movie[0]] = neighbor[0]
			else:
				recommand_dict[movie[0]] += neighbor[0]

	#建立推荐列表
	recommand_list = []
	for key in recommand_dict:
		recommand_list.append([recommand_dict[key], key])
	recommand_list.sort(reverse = True)
	user_movies = [k[0] for k in userDict[userId]]
	return [k[1] for k in recommand_list], user_movies, itemUser, neighbors

#获取电影的列表
def getMovieList(filename):
	contents = readFile(filename)
	movies_info = {}
	for movie in contents:
		single_info = movie.split("|")
		movies_info[int(single_info[0])] = single_info[1:]
	return movies_info

#从这里开始运行	
if __name__ == '__main__':

	reload(sys)
	sys.setdefaultencoding('utf-8')
	
	#获取所有电影的列表
	movies = getMovieList("u.item")
	recommend_list, user_movie, items_movie, neighbors = recommendByUserFC("u.data", 50, 80)
	neighbors_id=[ i[1] for i in neighbors]
	table = Texttable()
	table.set_deco(Texttable.HEADER)
	table.set_cols_dtype(['t', 't', 't'])
	table.set_cols_align(["l", "l", "l"])
	rows=[]
	rows.append([u"movie name",u"release", u"from userid"])
	for movie_id in recommend_list[:20]:
		from_user=[]
		for user_id in items_movie[movie_id]:
			if user_id in neighbors_id:
				from_user.append(user_id)
		rows.append([movies[movie_id][0],movies[movie_id][1],""])
	table.add_rows(rows)
	print table.draw()
         </textarea>
        </div>
        <div class="crayon-main" style="">
         <table class="crayon-table">
          <tbody>
           <tr class="crayon-row">
            <td class="crayon-nums " data-settings="show">
             <div class="crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-1">
               1
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-2">
               2
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-3">
               3
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-4">
               4
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-5">
               5
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-6">
               6
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-7">
               7
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-8">
               8
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-9">
               9
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-10">
               10
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-11">
               11
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-12">
               12
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-13">
               13
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-14">
               14
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-15">
               15
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-16">
               16
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-17">
               17
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-18">
               18
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-19">
               19
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-20">
               20
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-21">
               21
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-22">
               22
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-23">
               23
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-24">
               24
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-25">
               25
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-26">
               26
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-27">
               27
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-28">
               28
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-29">
               29
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-30">
               30
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-31">
               31
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-32">
               32
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-33">
               33
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-34">
               34
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-35">
               35
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-36">
               36
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-37">
               37
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-38">
               38
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-39">
               39
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-40">
               40
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-41">
               41
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-42">
               42
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-43">
               43
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-44">
               44
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-45">
               45
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-46">
               46
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-47">
               47
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-48">
               48
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-49">
               49
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-50">
               50
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-51">
               51
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-52">
               52
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-53">
               53
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-54">
               54
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-55">
               55
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-56">
               56
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-57">
               57
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-58">
               58
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-59">
               59
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-60">
               60
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-61">
               61
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-62">
               62
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-63">
               63
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-64">
               64
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-65">
               65
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-66">
               66
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-67">
               67
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-68">
               68
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-69">
               69
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-70">
               70
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-71">
               71
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-72">
               72
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-73">
               73
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-74">
               74
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-75">
               75
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-76">
               76
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-77">
               77
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-78">
               78
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-79">
               79
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-80">
               80
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-81">
               81
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-82">
               82
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-83">
               83
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-84">
               84
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-85">
               85
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-86">
               86
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-87">
               87
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-88">
               88
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-89">
               89
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-90">
               90
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-91">
               91
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-92">
               92
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-93">
               93
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-94">
               94
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-95">
               95
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-96">
               96
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-97">
               97
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-98">
               98
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-99">
               99
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-100">
               100
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-101">
               101
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-102">
               102
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-103">
               103
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-104">
               104
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-105">
               105
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-106">
               106
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-107">
               107
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-108">
               108
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-109">
               109
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-110">
               110
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-111">
               111
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-112">
               112
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-113">
               113
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-114">
               114
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-115">
               115
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-116">
               116
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-117">
               117
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-118">
               118
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-119">
               119
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-120">
               120
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-121">
               121
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-122">
               122
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-123">
               123
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-124">
               124
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-125">
               125
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-126">
               126
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-127">
               127
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-128">
               128
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-129">
               129
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-130">
               130
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-131">
               131
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-132">
               132
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-133">
               133
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-134">
               134
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-135">
               135
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-136">
               136
              </div>
              <div class="crayon-num" data-line="crayon-57686d2c90495583695634-137">
               137
              </div>
              <div class="crayon-num crayon-striped-num" data-line="crayon-57686d2c90495583695634-138">
               138
              </div>
             </div>
            </td>
            <td class="crayon-code">
             <div class="crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
              <div class="crayon-line" id="crayon-57686d2c90495583695634-1">
               <span class="crayon-p">
                # -*-coding=utf-8 -*-
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-2">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-3">
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                sys
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-4">
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-e">
                math
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-5">
               <span class="crayon-e">
                from
               </span>
               <span class="crayon-e">
                texttable
               </span>
               <span class="crayon-e">
                import
               </span>
               <span class="crayon-v">
                Texttable
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-6">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-7">
               <span class="crayon-p">
                #计算余弦距离
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-8">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                getCosDist
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                user1
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user2
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-9">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_x
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0.0
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-10">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_y
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0.0
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-11">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_xy
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0.0
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-12">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                key1
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user1
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-13">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                key2
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user2
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-14">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key1
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                ==
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key2
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-15">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_x
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +=
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key1
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key1
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-16">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_y
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +=
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key2
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key2
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-17">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_xy
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +=
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key1
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                *
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key2
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-18">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_xy
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                ==
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0.0
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-19">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                0
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-20">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                demo
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                math
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                sqrt
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-e ">
                sum_x *
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_y
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-21">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sum_xy
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                /
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                demo
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-22">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-23">
               <span class="crayon-p">
                #读取文件，读取以行为单位，每一行是列表里的一个元素
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-24">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                readFile
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                filename
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-25">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                contents
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-26">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                f
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                open
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                filename
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                "r"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-27">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                contents
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                f
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                readlines
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-28">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                f
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                close
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-29">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                contents
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-30">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-31">
               <span class="crayon-p">
                #数据格式化为二维数组
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-32">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                getRatingInfo
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                ratings
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-33">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rates
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-34">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                line
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                ratings
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-35">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rate
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                line
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                split
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "\t"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-36">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rates
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-t">
                int
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rate
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                int
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rate
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                int
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rate
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-37">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rates
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-38">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-39">
               <span class="crayon-p">
                #生成用户评分数据结构
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-40">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                getUserScoreDataStructure
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rates
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-41">
               <span class="crayon-h">
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-42">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #userDict[2]=[(1,5),(4,2)].... 表示用户2对电影1的评分是5，对电影4的评分是2
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-43">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                {
               </span>
               <span class="crayon-sy">
                }
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-44">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                {
               </span>
               <span class="crayon-sy">
                }
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-45">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rates
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-46">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user_rank
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                2
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-47">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-48">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                user_rank
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-49">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                else
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-50">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                user_rank
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-51">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-52">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-53">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-54">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                else
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-55">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-56">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-57">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-58">
               <span class="crayon-p">
                #计算与指定用户最相近的邻居
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-59">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                getNearestNeighbor
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                userId
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-60">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-61">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                item
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                userId
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-62">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                neighbor
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                item
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-63">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                !=
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                userId
               </span>
               <span class="crayon-st">
                and
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                neighbor
               </span>
               <span class="crayon-st">
                not
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-64">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-65">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors_dist
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-66">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                neighbor
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-67">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                dist
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                getCosDist
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                userId
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-68">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors_dist
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                dist
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-69">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors_dist
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                sort
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                reverse
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-70">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors_dist
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-71">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-72">
               <span class="crayon-p">
                #使用UserFC进行推荐，输入：文件名,用户ID,邻居数量
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-73">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                recommendByUserFC
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                filename
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userId
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                5
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-74">
               <span class="crayon-h">
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-75">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #读取文件
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-76">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                contents
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                readFile
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                filename
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-77">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-78">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #文件格式数据转化为二维数组
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-79">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rates
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                getRatingInfo
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                contents
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-80">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-81">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #格式化成字典数据
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-82">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                getUserScoreDataStructure
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rates
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-83">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-84">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #找邻居
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-85">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                getNearestNeighbor
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                userId
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                5
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-86">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-87">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #建立推荐字典
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-88">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_dict
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                {
               </span>
               <span class="crayon-sy">
                }
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-89">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                neighbor
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-90">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbor_user_id
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-91">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movies
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                neighbor_user_id
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-92">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                movie
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movies
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-93">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movie
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                not
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_dict
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-94">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_dict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                movie
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-95">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                else
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-96">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_dict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                movie
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                +=
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbor
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-97">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-98">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #建立推荐列表
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-99">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_list
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-100">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                key
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_dict
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-101">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_list
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                recommand_dict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                key
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                key
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-102">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_list
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                sort
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                reverse
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-t">
                True
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-103">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user_movies
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                userDict
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                userId
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-104">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                k
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                k
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommand_list
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user_movies
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                itemUser
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-105">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-106">
               <span class="crayon-p">
                #获取电影的列表
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-107">
               <span class="crayon-e">
                def
               </span>
               <span class="crayon-e">
                getMovieList
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                filename
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-108">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                contents
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                readFile
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                filename
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-109">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movies_info
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-sy">
                {
               </span>
               <span class="crayon-sy">
                }
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-110">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                movie
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                contents
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-111">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                single_info
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movie
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                split
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "|"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-112">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movies_info
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-t">
                int
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                single_info
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                single_info
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-113">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                return
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movies_info
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-114">
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-115">
               <span class="crayon-p">
                #从这里开始运行
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-116">
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                __name__
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                ==
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                '__main__'
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-117">
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-118">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                reload
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                sys
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-119">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                sys
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                setdefaultencoding
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                'utf-8'
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-120">
               <span class="crayon-h">
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-121">
               <span class="crayon-h">
               </span>
               <span class="crayon-p">
                #获取所有电影的列表
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-122">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                movies
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                getMovieList
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "u.item"
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-123">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommend_list
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                user_movie
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                items_movie
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                recommendByUserFC
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-s">
                "u.data"
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                50
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-cn">
                80
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-124">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors_id
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                i
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                i
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-125">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                table
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                Texttable
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-126">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                table
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                set_deco
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                Texttable
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-v">
                HEADER
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-127">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                table
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                set_cols_dtype
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-s">
                't'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                't'
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                't'
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-128">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                table
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                set_cols_align
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-s">
                "l"
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                "l"
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-s">
                "l"
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-129">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rows
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-130">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rows
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-i">
                u
               </span>
               <span class="crayon-s">
                "movie name"
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-i">
                u
               </span>
               <span class="crayon-s">
                "release"
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-i">
                u
               </span>
               <span class="crayon-s">
                "from userid"
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-131">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                movie_id
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                recommend_list
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-o">
                :
               </span>
               <span class="crayon-cn">
                20
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-132">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                from_user
               </span>
               <span class="crayon-o">
                =
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-sy">
                ]
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-133">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                for
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                user_id
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                items_movie
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                movie_id
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-134">
               <span class="crayon-h">
               </span>
               <span class="crayon-st">
                if
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                user_id
               </span>
               <span class="crayon-st">
                in
               </span>
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                neighbors_id
               </span>
               <span class="crayon-o">
                :
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-135">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                from_user
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                user_id
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-136">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                rows
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                append
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                movies
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                movie_id
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                0
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-v">
                movies
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-v">
                movie_id
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                [
               </span>
               <span class="crayon-cn">
                1
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                ,
               </span>
               <span class="crayon-s">
                ""
               </span>
               <span class="crayon-sy">
                ]
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line" id="crayon-57686d2c90495583695634-137">
               <span class="crayon-h">
               </span>
               <span class="crayon-v">
                table
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                add_rows
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-v">
                rows
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
              <div class="crayon-line crayon-striped-line" id="crayon-57686d2c90495583695634-138">
               <span class="crayon-h">
               </span>
               <span class="crayon-e">
                print
               </span>
               <span class="crayon-v">
                table
               </span>
               <span class="crayon-sy">
                .
               </span>
               <span class="crayon-e">
                draw
               </span>
               <span class="crayon-sy">
                (
               </span>
               <span class="crayon-sy">
                )
               </span>
              </div>
             </div>
            </td>
           </tr>
          </tbody>
         </table>
        </div>
       </div>
       <!-- [Format Time: 0.1123 seconds] -->
       <p>
        推荐结果如下
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        接下来再来看一个题目，这个题目是2014年阿里的大数据竞赛题目，描述可以参考如下链接
       </p>
       <p>
       </p>
       <p>
        <strong>
         题目：
        </strong>
        <a href="http://102.alibaba.com/competition/addDiscovery/gameIntroduce.htm" target="_blank">
         <strong>
          http://102.alibaba.com/competition/addDiscovery/gameIntroduce.htm
         </strong>
        </a>
       </p>
       <p>
       </p>
       <p>
        <strong>
         题意：
        </strong>
        根据用户在天猫的4个月的行为日志，建立用户的品牌偏好，并预测他们在接下来的一个月内对品牌商品的
       </p>
       <p>
        购买行为。开放的字段类型如下
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        <strong>
         解析：
        </strong>
        <a href="http://www.tuicool.com/articles/AN7Rf2" target="_blank">
         <strong>
          http://www.tuicool.com/articles/AN7Rf2
         </strong>
        </a>
       </p>
       <p>
       </p>
       <p>
        上面的是建立的简单的模型，实际上在天猫，有基于行为簇的用户偏好分析。
       </p>
       <p>
       </p>
       <p>
       </p>
       <p>
        <strong>
         协同过滤资料
        </strong>
       </p>
       <p>
       </p>
       <p>
        <strong>
         <a href="http://www.ibm.com/developerworks/cn/web/1103_zhaoct_recommstudy2/index.html" target="_blank">
          1. 推荐引擎算法
         </a>
        </strong>
       </p>
       <p>
        <strong>
         <a href="http://www.oschina.net/news/51297/top-10-open-source-recommendation-systems" target="_blank">
          2. 开源推荐引擎框架
         </a>
        </strong>
       </p>
       <p>
        <strong>
         <a href="http://blog.163.com/lnhenrylee@126/blog/static/2414832520123269713813/" target="_blank">
          3. 协调过滤介绍
         </a>
        </strong>
       </p>
       <p>
        <strong>
         <a href="http://zh.wikipedia.org/wiki/Slope_one" target="_blank">
          4. Slop one
         </a>
        </strong>
       </p>
       <p>
       </p>
       <p>
       </p>
       <div>
       </div>
       <div>
        文章出处：http://blog.csdn.net/acdreamers/article/details/44672305
       </div>
      </div>
      <div>
       <strong>
        注：转载文章均来自于公开网络，仅供学习使用，不会用于任何商业用途，如果侵犯到原作者的权益，请您与我们联系删除或者授权事宜，联系邮箱：contact@dataunion.org。转载数盟网站文章请注明原文章作者，否则产生的任何版权纠纷与数盟无关。
       </strong>
      </div>
      <!--content_text-->
      <div class="fenxian">
       <!-- JiaThis Button BEGIN -->
       <div class="jiathis_style_32x32">
        <p class="jiathis_button_weixin">
        </p>
        <p class="jiathis_button_tsina">
        </p>
        <p class="jiathis_button_qzone">
        </p>
        <p class="jiathis_button_cqq">
        </p>
        <p class="jiathis_button_tumblr">
        </p>
        <a class="jiathis jiathis_txt jtico jtico_jiathis" href="http://www.jiathis.com/share" target="_blank">
        </a>
        <p class="jiathis_counter_style">
        </p>
       </div>
       <!-- JiaThis Button END -->
      </div>
     </article>
     <!--content-->
     <!--相关文章-->
     <div class="xianguan">
      <div class="xianguantitle">
       相关文章！
      </div>
      <ul class="pic">
       <li>
        <a href="http://dataunion.org/20483.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/08/NF70-300x154.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20483.html" rel="bookmark" title="海盗动态早知道，美国海军研发出预测算法“海盗副本”">
         海盗动态早知道，美国海军研发出预测算法“海盗副本”
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/20441.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/08/9fa9ef344f_副本-300x124.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/20441.html" rel="bookmark" title="为什么“高大上”的算法工程师变成了数据民工？">
         为什么“高大上”的算法工程师变成了数据民工？
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/19959.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/07/t0162af9a5355e725de_副本.jpg"/>
        </a>
        <a class="link" href="http://dataunion.org/19959.html" rel="bookmark" title="GPU基础算法: Reduce, Scan, Histogram">
         GPU基础算法: Reduce, Scan, Histogram
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/17038.html">
         <img src="http://dataunion.org/wp-content/uploads/2015/05/t013fc4f5dfebdae1f8_副本.png"/>
        </a>
        <a class="link" href="http://dataunion.org/17038.html" rel="bookmark" title="基于混沌的数字图像加密算法">
         基于混沌的数字图像加密算法
        </a>
       </li>
      </ul>
     </div>
     <!--相关文章-->
     <div class="comment" id="comments">
      <!-- You can start editing here. -->
      <!-- If comments are open, but there are no comments. -->
      <div class="title">
       期待你一针见血的评论，Come on！
      </div>
      <div id="respond">
       <p>
        不用想啦，马上
        <a href="http://dataunion.org/wp-login.php?redirect_to=http%3A%2F%2Fdataunion.org%2F13503.html">
         "登录"
        </a>
        发表自已的想法.
       </p>
      </div>
     </div>
     <!-- .nav-single -->
    </div>
    <!--Container End-->
    <aside id="sitebar">
     <div class="sitebar_list2">
      <div class="wptag">
       <span class="tagtitle">
        热门标签+
       </span>
       <div class="tagg">
        <ul class="menu" id="menu-%e5%8f%8b%e6%83%85%e9%93%be%e6%8e%a5">
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-1605" id="menu-item-1605">
          <a href="http://taidizh.com/">
           泰迪智慧
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20884" id="menu-item-20884">
          <a href="http://www.transwarp.cn/">
           星环科技
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-3538" id="menu-item-3538">
          <a href="http://datall.org/">
           珈和遥感
          </a>
         </li>
         <li class="menu-item menu-item-type-custom menu-item-object-custom menu-item-20888" id="menu-item-20888">
          <a href="http://www.chinahadoop.cn/">
           小象学院
          </a>
         </li>
        </ul>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <div class="textwidget">
       <div align="center">
        <a href="http://study.163.com/course/courseMain.htm?courseId=991022" target="_blank">
         <img src="http://dataunion.org/wp-content/uploads/2016/03/dv.jpg"/>
        </a>
       </div>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       文章分类
      </h4>
      <div class="tagcloud">
       <a class="tag-link-44" href="http://dataunion.org/category/industry/demo" style="font-size: 10.204724409449pt;" title="4个话题">
        Demo展示
       </a>
       <a class="tag-link-31" href="http://dataunion.org/category/experts" style="font-size: 15.826771653543pt;" title="52个话题">
        专家团队
       </a>
       <a class="tag-link-870" href="http://dataunion.org/category/tech/ai" style="font-size: 19.795275590551pt;" title="273个话题">
        人工智能
       </a>
       <a class="tag-link-488" href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f" style="font-size: 8pt;" title="1个话题">
        加入数盟
       </a>
       <a class="tag-link-869" href="http://dataunion.org/category/tech/viz" style="font-size: 17.204724409449pt;" title="93个话题">
        可视化
       </a>
       <a class="tag-link-30" href="http://dataunion.org/category/partners" style="font-size: 10.645669291339pt;" title="5个话题">
        合作伙伴
       </a>
       <a class="tag-link-889" href="http://dataunion.org/category/parterc" style="font-size: 11.582677165354pt;" title="8个话题">
        合作会议
       </a>
       <a class="tag-link-104" href="http://dataunion.org/category/books" style="font-size: 12.96062992126pt;" title="15个话题">
        图书
       </a>
       <a class="tag-link-220" href="http://dataunion.org/category/tech/base" style="font-size: 19.850393700787pt;" title="281个话题">
        基础架构
       </a>
       <a class="tag-link-219" href="http://dataunion.org/category/tech/analysis" style="font-size: 19.409448818898pt;" title="232个话题">
        数据分析
       </a>
       <a class="tag-link-887" href="http://dataunion.org/category/tech/dm" style="font-size: 13.291338582677pt;" title="17个话题">
        数据挖掘
       </a>
       <a class="tag-link-34" href="http://dataunion.org/category/tech" style="font-size: 20.732283464567pt;" title="404个话题">
        文章
       </a>
       <a class="tag-link-1" href="http://dataunion.org/category/uncategorized" style="font-size: 22pt;" title="693个话题">
        未分类
       </a>
       <a class="tag-link-4" href="http://dataunion.org/category/events" style="font-size: 14.503937007874pt;" title="29个话题">
        活动
       </a>
       <a class="tag-link-890" href="http://dataunion.org/category/tech/%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0" style="font-size: 10.204724409449pt;" title="4个话题">
        深度学习
       </a>
       <a class="tag-link-221" href="http://dataunion.org/category/tech/devl" style="font-size: 18.968503937008pt;" title="193个话题">
        编程语言
       </a>
       <a class="tag-link-888" href="http://dataunion.org/category/career" style="font-size: 15.661417322835pt;" title="48个话题">
        职业规划
       </a>
       <a class="tag-link-5" href="http://dataunion.org/category/jobs" style="font-size: 14.11811023622pt;" title="25个话题">
        职位
       </a>
       <a class="tag-link-871" href="http://dataunion.org/category/industry" style="font-size: 15.716535433071pt;" title="49个话题">
        行业
       </a>
       <a class="tag-link-613" href="http://dataunion.org/category/industry/case" style="font-size: 16.984251968504pt;" title="84个话题">
        行业应用
       </a>
       <a class="tag-link-885" href="http://dataunion.org/category/industry/news" style="font-size: 17.425196850394pt;" title="102个话题">
        行业资讯
       </a>
       <a class="tag-link-10" href="http://dataunion.org/category/training" style="font-size: 14.228346456693pt;" title="26个话题">
        课程
       </a>
       <a class="tag-link-16" href="http://dataunion.org/category/sources" style="font-size: 15.661417322835pt;" title="48个话题">
        资源
       </a>
      </div>
     </div>
     <div class="sitebar_list">
      <h4 class="sitebar_title">
       功能
      </h4>
      <ul>
       <li>
        <a href="http://dataunion.org/wp-login.php?action=register">
         注册
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/wp-login.php">
         登录
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/feed">
         文章
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="http://dataunion.org/comments/feed">
         评论
         <abbr title="Really Simple Syndication">
          RSS
         </abbr>
        </a>
       </li>
       <li>
        <a href="https://cn.wordpress.org/" title="基于WordPress，一个优美、先进的个人信息发布平台。">
         WordPress.org
        </a>
       </li>
      </ul>
     </div>
    </aside>
    <div class="clear">
    </div>
   </div>
   <!--main-->
   ﻿
   <footer id="dibu">
    <div class="about">
     <div class="right">
      <ul class="menu" id="menu-%e5%ba%95%e9%83%a8%e8%8f%9c%e5%8d%95">
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-18024" id="menu-item-18024">
        <a href="http://dataunion.org/category/partners">
         合作伙伴
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20881" id="menu-item-20881">
        <a href="http://dataunion.org/contribute">
         文章投稿
        </a>
       </li>
       <li class="menu-item menu-item-type-taxonomy menu-item-object-category menu-item-20872" id="menu-item-20872">
        <a href="http://dataunion.org/category/%e5%8a%a0%e5%85%a5%e6%95%b0%e7%9b%9f">
         加入数盟
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22441" id="menu-item-22441">
        <a href="http://dataunion.org/f-links">
         友情链接
        </a>
       </li>
       <li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-20874" id="menu-item-20874">
        <a href="http://dataunion.org/aboutus">
         关于数盟
        </a>
       </li>
      </ul>
      <p class="banquan">
       数盟社区        ，
        做最棒的数据科学社区
      </p>
     </div>
     <div class="left">
      <ul class="bottomlist">
       <li>
        <a href="http://weibo.com/DataScientistUnion  " target="_blank" 　title="">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weibo.png"/>
        </a>
       </li>
       <li>
        <a class="cd-popup-trigger" href="http://dataunion.org/13503.html#0">
         <img src="http://dataunion.org/wp-content/themes/yzipi/images/weixin.png"/>
        </a>
       </li>
      </ul>
      <div class="cd-popup">
       <div class="cd-popup-container">
        <h1>
         扫描二维码,加微信公众号
        </h1>
        <img src="http://dataunion.org/wp-content/themes/yzipi/images/2014-12-06-1515289049.png"/>
        <a class="cd-popup-close" href="http://dataunion.org/13503.html">
        </a>
       </div>
       <!-- cd-popup-container -->
      </div>
      <!-- cd-popup -->
     </div>
    </div>
    <!--about-->
    <div class="bottom">
     <a href="http://dataunion.org/">
      数盟社区
     </a>
     <a href="http://www.miitbeian.gov.cn/" rel="external nofollow" target="_blank">
      京ICP备14026740号
     </a>
     联系我们：
     <a href="mailto:contact@dataunion.org" target="_blank">
      contact@dataunion.org
     </a>
     <div class="tongji">
     </div>
     <!--bottom-->
     <div class="scroll" id="scroll" style="display:none;">
      ︿
     </div>
    </div>
   </footer>
   <!--dibu-->
  </div>
 </body>
</html>